//
//  ATTMBiddingManager.m
//  HeadBiddingDemo
//
//  Created by lix on 2022/10/20.
//

#import "CJATTMBiddingManager.h"
#import "CJATTMBiddingDelegate.h"

@interface CJATTMBiddingManager ()

@property (nonatomic, strong) NSMutableDictionary *bidingAdStorageAccessor;
@property (nonatomic, strong) NSMutableDictionary *bidingAdDelegate;

@end

@implementation CJATTMBiddingManager

+ (instancetype)sharedInstance {
    static CJATTMBiddingManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[CJATTMBiddingManager alloc] init];
        sharedInstance.bidingAdStorageAccessor = [NSMutableDictionary dictionary];
        sharedInstance.bidingAdDelegate = [NSMutableDictionary dictionary];
    });
    return sharedInstance;
}

- (CJATTMBiddingRequest *)getRequestItemWithUnitID:(NSString *)unitID {
    @synchronized (self) {
        return [self.bidingAdStorageAccessor objectForKey:unitID];
    }
    
}

- (void)removeRequestItmeWithUnitID:(NSString *)unitID {
    @synchronized (self) {
        [self.bidingAdStorageAccessor removeObjectForKey:unitID];
    }
}

- (void)savaBiddingDelegate:(CJATTMBiddingDelegate *)delegate withUnitID:(NSString *)unitID {
    @synchronized (self) {
        [self.bidingAdDelegate setObject:delegate forKey:unitID];
    }
}

- (void)removeBiddingDelegateWithUnitID:(NSString *)unitID {
    @synchronized (self) {
        if (unitID.length) {
            [self.bidingAdDelegate removeObjectForKey:unitID];
        }
    }
}

- (void)startWithRequestItem:(CJATTMBiddingRequest *)request {
    
    [self.bidingAdStorageAccessor setObject:request forKey:request.unitID];
    CJATTMBiddingDelegate *delegate = [[CJATTMBiddingDelegate alloc] init];
    delegate.callbackAssets = ^(NSArray * _Nonnull assets) {
        request.assets = assets;
    };
    delegate.callbackBannerView = ^(UIView * _Nonnull bannerView) {
        request.bannerView = bannerView;
    };
    delegate.unitID = request.unitID;
    [request.customObject setValue:delegate forKey:@"delegate"];
    [self savaBiddingDelegate:delegate withUnitID:request.unitID];
    
    
//    switch (request.adType) {
//        case ESCAdFormatSplash: 
//        {
//            CJATTMBiddingDelegate *delegate = [[CJATTMBiddingDelegate alloc] init];
//            delegate.unitID = request.unitID;
//            [request.customObject setValue:delegate forKey:@"delegate"];
//            [self savaBiddingDelegate:delegate withUnitID:request.unitID];
//            break;
//        }
//        case ESCAdFormatInterstitial:
//        {
//            CJATTMBiddingDelegate *delegate = [[CJATTMBiddingDelegate alloc] init];
//            delegate.unitID = request.unitID;
//            [request.customObject setValue:delegate forKey:@"delegate"];
//            [self savaBiddingDelegate:delegate withUnitID:request.unitID];
//            break;
//        }
//        default:
//            break;
//    }
}


@end
